package com.daka.controller;

import com.daka.entity.Activity;
import com.daka.entity.Volunteer;
import com.daka.enumerate.StatusCode;
import com.daka.service.ActivityService;
import com.daka.service.VolunteerService;
import com.daka.tools.DateUtil;
import com.daka.vo.request.AddressActVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.org.ActVolunteerVo;
import com.daka.vo.response.org.AddressActDetResponseVo;
import com.daka.vo.response.org.AddressActResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/12/6 9:15
 */
@Controller
@RequestMapping("/addressAct")
@Slf4j
public class AddressActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private VolunteerService volunteerService;

    //列表
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryList", consumes = "application/json")
    public DataResponseVo<Object> queryList(@RequestBody AddressActVo vo) {
        if (StringUtils.isNotEmpty(vo.getTags())) {
            vo.setTagsTemp(vo.getTags().split(","));
        }
        if ((vo.getStartTime() != null && vo.getEndTime() == null) || (vo.getStartTime() == null && vo.getEndTime() != null)) {
            return new DataResponseVo<>(StatusCode.PARAM_ERROR.getStatusCode(), StatusCode.PARAM_ERROR.getStatusMessage());
        }
        if (vo.getStartTime() != null && vo.getEndTime() != null) {
            try {
                if (!DateUtil.ifbefore(vo.getStartTime(), vo.getEndTime())) {
                    return new DataResponseVo<>(StatusCode.DATE_FORMAT_ERROR.getStatusCode(), "查询时间段的开始时间应该小于结束时间！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                return new DataResponseVo<>(StatusCode.DATE_FORMAT_ERROR.getStatusCode(), StatusCode.DATE_FORMAT_ERROR.getStatusMessage());
            }

        }
        List<Activity> list = activityService.selectByAddressTemp(vo);
        List<AddressActResponseVo> retList = new ArrayList<>();
        if (list.size() > 0) {
            for (Activity activity : list) {
                AddressActResponseVo addressAct = AddressActResponseVo.build(activity);
                Volunteer volunteer = volunteerService.selectById(activity.getMonitor());
                if (volunteer != null) {
                    addressAct.setMonitor(volunteer.getRealName());
                    addressAct.setMobile(volunteer.getMobile());
                }
                retList.add(addressAct);
            }
        }
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), retList);
    }


    //详情
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryDet", consumes = "application/json")
    public DataResponseVo<Object> queryDet(@RequestBody Activity activity) {
        if (activity.getId() == 0) {
            return new DataResponseVo<>(StatusCode.PARAM_ERROR.getStatusCode(), StatusCode.PARAM_ERROR.getStatusMessage());
        }
        Activity activity1 = activityService.selectById(activity.getId());
        AddressActDetResponseVo addressActDet = null;
        if (activity1 != null) {
            addressActDet = AddressActDetResponseVo.build(activity1);
            Volunteer volunteer = volunteerService.selectById(activity1.getMonitor());
            if (volunteer != null) {
                addressActDet.setMonitor(volunteer.getRealName());
                addressActDet.setMobile(volunteer.getMobile());
            }
            //查询志愿者
            List<ActVolunteerVo> listVo = volunteerService.selectByAct(activity1.getId());
            addressActDet.setListVol(listVo);
        }
        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), addressActDet);
    }

}





